﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using DataBase;

namespace YJKDataLookAfter
{
	public class DbOpening : DbObject
	{
		[Category("属性")]
		[Description("建筑模型Id")]
		[DisplayName("建筑模型Id")]
		[ReadOnly(true)]
		public int RacId
		{
			get
			{
				return this.mRacId;
			}
			set
			{
				this.mRacId = value;
			}
		}

		[Category("属性")]
		[Description("模型Id")]
		[DisplayName("模型Id")]
		[ReadOnly(true)]
		public int ModleId
		{
			get
			{
				return this.mModleId;
			}
			set
			{
				this.mModleId = value;
			}
		}

		[Category("属性")]
		[Description("洞口边界路径")]
		[DisplayName("洞口边界路径")]
		[ReadOnly(true)]
		public DbEdgeLoop EdgeLoop
		{
			get
			{
				return this.mEdgeLoop;
			}
		}

		[Category("属性")]
		[Description("洞口类型")]
		[DisplayName("洞口类型")]
		[ReadOnly(true)]
		public OpeningType OpeningType
		{
			get
			{
				return this.mOpeningType;
			}
		}

		[Category("属性")]
		[Description("建筑模型路径")]
		[DisplayName("建筑模型路径")]
		[ReadOnly(true)]
		public string ArchPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, this.mDatabase).RacFilePath;
			}
		}

		[Category("属性")]
		[Description("模型路径")]
		[DisplayName("模型路径")]
		[ReadOnly(true)]
		public string ModelPath
		{
			get
			{
				DbProject fromDB = DbProject.GetFromDB(this.mProjectId, this.mDatabase);
				string result = "";
				if (this.mOpeningType == OpeningType.Rst)
				{
					result = fromDB.RstFilePath;
				}
				else if (this.mOpeningType == OpeningType.Rme)
				{
					result = fromDB.RmeFilePath;
				}
				return result;
			}
		}

		public DbOpening(int projectId, DbEdgeLoop del, int racId, int modleId, OpeningType openingType)
		{
			this.mProjectId = projectId;
			this.mRacId = racId;
			this.mModleId = modleId;
			this.mOpeningType = openingType;
			this.mEdgeLoop = del;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				this.mEdgeLoop.AddToDb(db);
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbopening (ProjectId,RacId, ModelId,EdgeLoopId,OpeningType) VALUES ({0},{1},{2},{3},{4})", new object[]
				{
					this.mProjectId,
					this.mRacId,
					this.mModleId,
					this.mEdgeLoop.Id,
					(int)this.mOpeningType
				});
				db.ExecuteCommand(sql);
				string sql2 = string.Format("select max(Id) from tbopening", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public static DbOpening GetFromDB(int id, IDatabase db)
		{
			DbOpening result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT ProjectId,RacId, ModelId,EdgeLoopId,OpeningType FROM tbopening WHERE ID={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					int projectId = Convert.ToInt32(dataTable.Rows[0]["ProjectId"]);
					int racId = Convert.ToInt32(dataTable.Rows[0]["RacId"]);
					int modleId = Convert.ToInt32(dataTable.Rows[0]["ModelId"]);
					int edgeLoopId = Convert.ToInt32(dataTable.Rows[0]["EdgeLoopId"]);
					DbEdgeLoop fromDB = DbEdgeLoop.GetFromDB(edgeLoopId, db);
					OpeningType openingType = (OpeningType)Convert.ToInt32(dataTable.Rows[0]["OpeningType"]);
					DbOpening dbOpening = new DbOpening(projectId, fromDB, racId, modleId, openingType);
					DbEdgeLoop fromDB2 = DbEdgeLoop.GetFromDB(edgeLoopId, db);
					dbOpening.EdgeLoop.AddLoop(fromDB2);
					dbOpening.mId = id;
					dbOpening.mDatabase = db;
					result = dbOpening;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbOpening> GetByProject(DbProject project)
		{
			List<DbOpening> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbOpening> list = new List<DbOpening>();
				string sql = string.Format("SELECT ProjectId,RacId, ModelId,EdgeLoopId,OpeningType FROM tbopening WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacId"]);
						int modleId = Convert.ToInt32(dataTable.Rows[i]["ModelId"]);
						DbEdgeLoop fromDB = DbEdgeLoop.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["EdgeLoopId"]), project.Database);
						OpeningType openingType = (OpeningType)Convert.ToInt32(dataTable.Rows[i]["OpeningType"]);
						DbOpening item = new DbOpening(projectId, fromDB, racId, modleId, openingType);
						list.Add(item);
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbopening SET ProjectId={0},RacId = {1},ModelId={2},EdgeLoopId={3},OpeningType={4} WHERE Id ={5}", new object[]
				{
					this.mProjectId,
					this.mRacId,
					this.mModleId,
					this.mEdgeLoop.Id,
					(int)this.mOpeningType,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				string sql = string.Format("DELETE FROM tbopening WHERE Id = {0}", base.Id);
				this.mEdgeLoop.RemoveFromDb();
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		private int mProjectId;

		private int mRacId;

		private int mModleId;

		private DbEdgeLoop mEdgeLoop = new DbEdgeLoop();

		public OpeningType mOpeningType;
	}
}
